package wec_wallet.ebo.com.activity.wallet;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.http_utils.utils.NetState;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.activity.MineActivity;
import wec_wallet.ebo.com.activity.capital.ChangeIntoActivity;
import wec_wallet.ebo.com.activity.capital.TurnOutActivity;
import wec_wallet.ebo.com.adapter.WalletAdapter;
import wec_wallet.ebo.com.bean.WalletBean;
import wec_wallet.ebo.com.bean.WalletDateBean;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.intercept.PhoneListenService;
import wec_wallet.ebo.com.utils.Functions;

/**
 * 钱包主界面
 * Created by Administrator on 2018/3/27.
 */

public class
WalletActivity extends MyBaseActivity {

    @BindView(R.id.tv_wallet_total)
    TextView tvWalletTotal;
    @BindView(R.id.rela_wallet_into)
    RelativeLayout relaWalletInto;
    @BindView(R.id.rela_wallet_out)
    RelativeLayout relaWalletOut;
    @BindView(R.id.lv_wallet)
    ListView lvWallet;
    WalletAdapter adapter;
    List<WalletDateBean> list;
    private PhoneListenService service = null;
    private boolean isBound = false;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            isBound = true;
            service = ((PhoneListenService.MyBinder) binder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        ButterKnife.bind(this);
        initViews();

    }

    @Override
    public void initViews() {
        list = new ArrayList<>();
    }

    @OnItemClick(R.id.lv_wallet)
    public void onItemClick(int i) {
        Intent intent = new Intent(this, CoinDetailsActivity.class);
//        intent.putExtra("name", list.get(i).getCoin());
        startActivity(intent);
    }

    @OnClick({R.id.rela_wallet_record, R.id.rela_wallet_01, R.id.rela_wallet_into, R.id
            .rela_wallet_out})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rela_wallet_record:
                startActivity(new Intent(this, TransferRecordsActivity.class));
                break;
            case R.id.rela_wallet_01:
                Intent intent = new Intent(this, MineActivity.class);
                startActivity(intent);
                break;
            case R.id.rela_wallet_into:
                startActivity(new Intent(WalletActivity.this, ChangeIntoActivity.class));
                break;
            case R.id.rela_wallet_out:
                startActivity(new Intent(WalletActivity.this, TurnOutActivity.class));
                break;
        }
    }

    private void getData() {
        Log.e("--", "--");
        Map<String, Object> map = new HashMap<>();
        map.put("token", I_Share.readAccountToken());
        map.put("uid", I_Share.readAccountUserid());

        Functions.post(this, Constant.URL_PROPERTY, map, new
                ListenerProperty(), false, true);
    }

    private List<WalletDateBean> getWalletData(String json) {
        return new Gson().fromJson(json, WalletBean.class).getData();

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        mhandle.sendEmptyMessage(1);
    }



    private class ListenerProperty implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }

        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (null != result) {
                try {

                    JSONObject resultObject = new JSONObject(result);
                    String status = resultObject.getString("error");
                    if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                            ("0")) {
                        IToast.show(resultObject.getString("error_msg"));
                        Functions.relogin(WalletActivity.this);//登录过期
                        return;
                    }
                    if (resultObject.has("error_msg")) {
                        IToast.show(resultObject.getString("error_msg"));
                        return;
                    }
                    if (status.equals("0")) {
                        String count = resultObject.getString("user_count_cny");
                        tvWalletTotal.setText(count);
                        list = getWalletData(result);
                        adapter = new WalletAdapter(WalletActivity.this, list);
                        lvWallet.setAdapter(adapter);


                    }

                } catch (Exception e) {
                    // TODO: handle exception
//                    IToast.show(e.toString());
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            } else {
                Lg.d("黑名单列表 result=null");

            }
        }
    }

    Handler mhandle = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    int time = 5;
                    sendMessageDelayed(obtainMessage(1), time * 1000);
                    if (NetState.isNetworkAvailable(getApplicationContext())) {
                        getData();
                    }
                    break;


            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mhandle.removeMessages(1);

    }

    @Override
    protected void onPause() {

        super.onPause();
        mhandle.removeMessages(1);
    }
}
